package com.nhjf.dao.payLog;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.nhjf.dao.BaseDao;
import com.nhjf.model.payLog.PayLog;
import com.nhjf.pojo.qo.payLog.PayLogQo;
@Repository
public class PayLogDao extends BaseDao<PayLog, PayLogQo>{

	@Override
	protected Criteria buildCriteria(Criteria criteria, PayLogQo qo) {
		// TODO Auto-generated method stub
		criteria.setFetchMode("coursesSeries", qo.isSeachCoursesSeries()?FetchMode.JOIN:FetchMode.SELECT);	
		criteria.setFetchMode("user", qo.isSeachUser()?FetchMode.JOIN:FetchMode.SELECT);	
		if(StringUtils.isNotBlank(qo.getParameters())) {
			criteria.add(Restrictions.eq("parameters",qo.getParameters()));
		}
		if(qo.getPayLogId()!=null) {
			criteria.add(Restrictions.eq("id",qo.getPayLogId()));
		}
		if(qo.getPayType()!=null) {
			criteria.add(Restrictions.eq("payType",qo.getPayType()));
		}
		if(qo.getStatus()!=null) {
			criteria.add(Restrictions.eq("status",qo.getStatus()));
		}
		if(qo.getCoursesSeries()!=null) {
			criteria.add(Restrictions.eq("coursesSeries.id",qo.getCoursesSeries()));
		}
		if(qo.getUser()!=null) {
			criteria.add(Restrictions.eq("user.id",qo.getUser()));
		}
		if(StringUtils.isNotBlank(qo.getTitle())) {
			criteria.createAlias("coursesSeries", "coursesSeries");
			criteria.add(Restrictions.like("coursesSeries.totalTitle",qo.getTitle(),MatchMode.ANYWHERE));
		}
		if(qo.getPaymentSuccessfulTime()!=null) {
			criteria.add(Restrictions.eq("paymentSuccessfulTime",qo.getPaymentSuccessfulTime()));
		}
		if(StringUtils.isNotBlank(qo.getCreateTime())) {
			Date date=null;
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			try {
				date=sdf.parse(qo.getCreateTime());
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			criteria.add(Restrictions.gt("createTime",date));
		}
		if(StringUtils.isNotBlank(qo.getCreateTimeEnd())) {
			Date date=null;
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			try {
				date=sdf.parse(qo.getCreateTimeEnd());
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			criteria.add(Restrictions.lt("createTime",date));
		}
		return criteria;
	}

	@Override
	protected Class<PayLog> getEntityClass() {
		// TODO Auto-generated method stub
		return PayLog.class;
	}

}
